package com.es.service.impl;

import com.es.service.QueryService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author 咕唧
 * @Date 2022/6/22 19:15
 * @Version 1.0
 */
@Service
public class QueryServiceImpl implements QueryService {
    private RestHighLevelClient client;

    private ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.client = restHighLevelClient;
    }

    @Override
    public SearchResponse termQuery(String index, String field, String value) throws IOException {
        //创建搜索的请求
        SearchResponse response = baseQuery(index, QueryBuilders.termQuery(field, value));
        return response;
    }

    @Override
    public SearchResponse termsQuery(String index, String field, String... value) throws IOException {
        SearchResponse response = baseQuery(index, QueryBuilders.termsQuery(field, value));
        return response;
    }

    @Override
    public SearchResponse matchAllQuery(String index) throws IOException {
        return baseQuery(index, QueryBuilders.matchAllQuery());
    }

    @Override
    public SearchResponse matchQuery(String index, String field, String value) throws IOException {
        return baseQuery(index, QueryBuilders.matchQuery(field, value));
    }

    @Override
    public SearchResponse boolQuery(String index, String field, String value, Operator operator) throws IOException {
        return baseQuery(index, QueryBuilders.matchQuery(field, value).operator(operator));
    }

    @Override
    public SearchResponse multiMatch(String index, String value, String... field) throws IOException {
        return baseQuery(index, QueryBuilders.multiMatchQuery(value, field));
    }


    /**
     * 因为我们的查询就是条件不一样,所以可以封装出来,只需要传递条件就行了
     *
     * @param index
     * @param queryBuilder
     * @return
     * @throws IOException
     */
    public SearchResponse baseQuery(String index, QueryBuilder queryBuilder) throws IOException {
        //创建搜索的请求
        SearchRequest searchRequest = new SearchRequest(index);
        //封装有查询条件的对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置term查询
        searchSourceBuilder.query(queryBuilder);
        //设置查询条件
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        return response;
    }
}
